<%@ page import="java.util.List" %>
<%@ page import="com.google.appengine.api.datastore.DatastoreServiceFactory" %>
<%@ page import="com.google.appengine.api.datastore.DatastoreService" %>
<%@ page import="com.google.appengine.api.datastore.Query" %>
<%@ page import="com.google.appengine.api.datastore.Entity" %>
<%@ page import="com.google.appengine.api.datastore.FetchOptions" %>
<%@ page import="com.google.appengine.api.datastore.Key" %>
<%@ page import="com.google.appengine.api.datastore.KeyFactory" %>
<%@ page import="com.google.appengine.api.datastore.Query" %>
<%@ page import="com.google.appengine.api.datastore.Query.FilterOperator" %>


<!-- Redirect to the login page if there is no session created -->
<%
if (session.getAttribute("username") == null) {
	response.sendRedirect("/login.jsp");
}
%>

    	<div class="header" style="margin-top:30px;">
			<h1>Search Results</h1>
		</div>
		
		<%
			DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
			Key accountKey = KeyFactory.createKey("Accounts", "accountName");
			
			String keyword = request.getParameter("keyword");
			Integer count=0;
			
			Query query = new Query("Account", accountKey);		
			List<Entity> UserAccounts = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(20));
		
			//COUNTING TOTAL NUMBER OF SEARCH RESULTS
			for (Entity accounts: UserAccounts) {
				String lowerusername = accounts.getProperty("username").toString().toLowerCase();
				String lowerFirstName = accounts.getProperty("firstName").toString().toLowerCase();
				String lowerLastName = accounts.getProperty("lastName").toString().toLowerCase();
				String lowerKeyword = keyword.toLowerCase();
				if ((lowerusername.indexOf(lowerKeyword)> -1) || (lowerFirstName.indexOf(lowerKeyword)> -1) || (lowerLastName.indexOf(lowerKeyword)> -1)) {
				count++;
				}
			}
		
		%> 
			
		<div id="results" align="center"> <%
			if(keyword == ""){
				%><p style="color: red;"> Please enter the username / First Name / Last Name of the user to search files</p></br><% 
			}
			else{
				%> <p style="/*background:66FF66; background-color:hsla(120,70%,80%,0.3);*/"> 
				Search results for <label style="font-weight: bold;">"<%=request.getParameter("keyword") %>"</label> 
				: <label style="font-weight: bold;"><%=count %></label>
				</p></br> <% 
			} %>
		</div>
			
		<div id="" style="width:auto; margin:20px 70px 0px 120px; padding:15px;">	
			<table class="table2">
			<thead><!--style="padding:5px; magin:5px;"-->
				<tr>
					<th style="width: 250px;">Username</th>
					<th style="width: 250px;">Password</th>
					<th style="width: 250px;">First Name</th>
					<th style="width: 250px;">Last Name</th>
					<th style="width: 200px;">User Role</th>
					<th style="width: 500px;">Lock Status</th>
					<th style="width: 200px;">Account Key</th>
					<th><img src="images/trash_bin.png" style="width: 40px; height: 40px; " alt="" /></th>
				</tr>
			</thead>
			<tbody>
			
			<%
			if(keyword == ""){
				%>
				<tr>
					<td colspan="9" scope="row" align="center" width="50%" height="100%" style="padding:20px;"> 
						<img alt="" src="/images/search_results.png" width="60" height="60"></br>
						No users found in the database
					</td>
				</tr>
				<%
			}
			else if (UserAccounts.isEmpty() && keyword != "") {
					%>
					<tr>
						<td colspan="9" scope="row" align="center" width="50%" height="100%" style="padding:20px;"> 
							<img alt="" src="/images/search_results.png" width="60" height="60"></br>
							No search results found for "<%=request.getParameter("keyword") %>"
						</td>
					</tr>
					<%				
				} else {
					for (Entity accounts: UserAccounts) {
						
						//ADD MORE LINES OF CODE TO SEARCH MORE ATTRIBUTES (eg. Searching in the artist, song, etc.)
						String lowerusername = accounts.getProperty("username").toString().toLowerCase();
						String lowerFirstName = accounts.getProperty("firstName").toString().toLowerCase();
						String lowerLastName = accounts.getProperty("lastName").toString().toLowerCase();
						
						
						String lowerKeyword = keyword.toLowerCase();
						if ((lowerusername.indexOf(lowerKeyword)> -1) || (lowerFirstName.indexOf(lowerKeyword)> -1) || (lowerLastName.indexOf(lowerKeyword)> -1)) {
							String keyParameter = KeyFactory.keyToString(accounts.getKey());
							%>
							<tr style="padding:5px;">
								<td align="left"><%= accounts.getProperty("username") %></td>
								<td align="left"><%= accounts.getProperty("password") %></td>
								<td align="left"><%= accounts.getProperty("firstName") %></td>
								<td align="center"><%= accounts.getProperty("lastName") %></td>
								<td align="center"><%= accounts.getProperty("account_type") %></td>
								<td align="center">
								<div class="status">
									<%if (accounts.getProperty("lockStatus").equals("Locked")){ %>
										<input type="button" value="Unlock" onclick="ajaxUnLockAccount('<%=accounts.getProperty("username").toString() %>', '<%= keyword %>')"/>
										<label id="locked"><%= accounts.getProperty("lockStatus") %></label>
									<%} %>
									<%if (accounts.getProperty("lockStatus").equals("Unlocked")){ %>
										<label id="unlocked"><%= accounts.getProperty("lockStatus") %></label>
										<input type="button" value="Lock" onclick="ajaxLockAccount('<%=accounts.getProperty("username").toString() %>', '<%= keyword %>')"/>
									<%} %>
								</div>
								</td>
								<td align="center"><%= accounts.getKey().toString() %></td>
								<td align="center">
									<input style="width:70px; height:34px;" type="button" value="Delete" onclick="ajaxDeleteAccount('<%=keyParameter %>', '<%= keyword %>')"/>
								</td>
							</tr>
							<%
						}
					}
				}
			%>
			</tbody>
			</table>
		</div>
